package com.jinhe.tss.cms;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.jinhe.tss.cms.dao.IArticleDao;
import com.jinhe.tss.cms.dao.IChannelDao;
import com.jinhe.tss.cms.entity.ArticleType;
import com.jinhe.tss.cms.service.IArticleService;
import com.jinhe.tss.cms.service.IArticleTypeService;
import com.jinhe.tss.cms.service.IChannelService;
import com.jinhe.tss.component.TxTestSupport;
import com.jinhe.tss.core.util.FileHelper;
import com.jinhe.tss.core.util.URLUtil;
import com.jinhe.tss.core.util.XMLDocUtil;
import com.jinhe.tss.um.TxSupportTest4UM;

/**
 * Junit Test 类里执行构造函数的时候无事务，即构造函数不在单元测试方法的事物边界内。
 */
@ContextConfiguration(
        locations={
          "classpath:spring/cms-applicationContext.xml",
          "classpath:spring/cms-upload-applicationContext.xml"
        } 
      )
@TransactionConfiguration(defaultRollback = false) // 不自动回滚，否则后续的test中没有初始化的数据
public abstract class TxSupportTest4CMS extends TxSupportTest4UM { 
	
    @Autowired protected IArticleTypeService articleTypeService;
    @Autowired protected IChannelService channelService;
    @Autowired protected IArticleService articleService;
    @Autowired protected IChannelDao channelDao;
    @Autowired protected IArticleDao articleDao;
    
    protected void displayPersistenceXML() {
        FileHelper.renameFile("META-INF/persistence.xml", "persistence.xml_cms");
    }
 
    /**
     * 初始化CMS的动态属性相关模板
     */
    protected void init() {
        super.init();
        
        /* 初始化一个普通文章类型 */
        initCommonArticleType();
    }
    
    /**
     * 初始化数据库脚本
     */
    protected void initSQL() {
    	super.initSQL();
    	TxTestSupport.excuteSQL(sqlpath + "/cms");
    }
    
    protected Document getApplicationPermissionConfig() {
        return XMLDocUtil.createDocByAbsolutePath(sqlpath + "/cms-application-config.xml");
    }
    
    private static final String ARTICLE_FILE_NALE = "ArticleProperties.xml";

    /** 注册动态属性实体（包括站点栏目、文章等） */
    protected void initDYN(){
        File path = new File(URLUtil.getResourceFileUrl(DYN_ENTITY_CONFIG_PATH).getFile());
        List<String> files = FileHelper.listFilesByType(".xml", path);
        for( String fileName : files ){
            if (!ARTICLE_FILE_NALE.equals(fileName)) {
                Document doc = XMLDocUtil.createDoc(DYN_ENTITY_CONFIG_PATH + "/" + fileName);
                propertyService.importDynableEntity(doc.asXML());
            }
        }
    }
    
    protected static ArticleType defaultArticleType;
    
    /** 初始化一个普通文章类型 */
    private void initCommonArticleType() {
        ArticleType articleTypeGroup = new ArticleType();
        articleTypeGroup.setName("基本类型组");
        articleTypeGroup.setParentId(CMSConstants.HEAD_NODE_ID);
        articleTypeGroup.setIsGroup(CMSConstants.TRUE);
        articleTypeGroup = articleTypeService.createArticleType(articleTypeGroup);
        
        defaultArticleType = new ArticleType();
        defaultArticleType.setName("默认文章类型");
        defaultArticleType.setParentId(articleTypeGroup.getId());
        defaultArticleType.setIsGroup(CMSConstants.FALSE);
        defaultArticleType = articleTypeService.createArticleType(defaultArticleType);
        
        // 将文章的模板导入到动态属性实体中
        Document doc = XMLDocUtil.createDoc(CMSConstants.TEMPLATE_ARTICLE_PROPERTY);
        Element root = doc.getRootElement();
        root.addAttribute("name", defaultArticleType.getName());
        root.addAttribute("code", defaultArticleType.getEntityCode());
        propertyService.importDynableEntity(doc.asXML());
    }
 
}
